using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;
namespace ContactsPlugIn
{
    [WriterPlugin
   ("95CC35E2-8FF7-49d0-B286-1710888C4585", "Contact Plugin",
        PublisherUrl = "http://www.visitmix.com",
     Description = "Insert Contacts Formatted As hCards", ImagePath = "microformats-16.png", HasEditableOptions = false)]
    [InsertableContentSource("Contact (hCard)")]
    public class Plugin : ContentSource
    {
        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
        {
            try
            {
                Contact c = this.GetContent(dialogOwner);
                if (c != null)
                {
                    content = FormatOutput(c);
                    return DialogResult.OK;
                }
                return DialogResult.Cancel;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return DialogResult.Cancel;
            }
            
        }
        private string FormatOutput(Contact contact)
        {
            // Used to format the ListViewItem data for final HTML output
            StringBuilder html = new StringBuilder();
            html.Append("<div class='vcard' >");
            if (!string.IsNullOrEmpty(contact.Photourl))
                html.AppendFormat("<img style='float:left; margin-right:4px' src='{0}' alt='photo of {1} {2}' class='photo'/>", contact.Photourl, contact.Givenname, contact.Familyname);
            html.Append("<span class='fn n'>");
            if (!string.IsNullOrEmpty(contact.Givenname))
                html.AppendFormat("<span class='given-name'>{0}</span> ", contact.Givenname);
            if (!string.IsNullOrEmpty(contact.Middlename))
                html.AppendFormat("<span class='additional-name'>{0}</span> ", contact.Middlename);
            if (!string.IsNullOrEmpty(contact.Familyname))
                html.AppendFormat("<span class='given-family'>{0}</span> ", contact.Familyname);
            html.Append("</span>");
            if (!string.IsNullOrEmpty(contact.Organization))
                html.AppendFormat("<div class='org'>{0}</div>", contact.Organization);
            if (!string.IsNullOrEmpty(contact.Email))
                html.AppendFormat("<div><a class='email' href='mailto:{0}'>{0}</a></div>", contact.Email);
            if (!string.IsNullOrEmpty(contact.Street) || !string.IsNullOrEmpty(contact.City) ||
                !string.IsNullOrEmpty(contact.State) || !string.IsNullOrEmpty(contact.Postalcode) ||
                !string.IsNullOrEmpty(contact.Countryname))
            {

            html.Append("<div class='adr'>");
            if (!string.IsNullOrEmpty(contact.Street))
                html.AppendFormat("<span class='street-address'>{0}</span> ", contact.Street);
            if (!string.IsNullOrEmpty(contact.City))
                html.AppendFormat("<span class='locality'>{0}</span>, ", contact.City);
            if (!string.IsNullOrEmpty(contact.State))
                html.AppendFormat("<span class='region'>{0}</span> ", contact.State);
            if (!string.IsNullOrEmpty(contact.Postalcode))
                html.AppendFormat("<span class='postal-code'>{0}</span> ", contact.Postalcode);
            if (!string.IsNullOrEmpty(contact.Countryname))
                html.AppendFormat("<span class='country-name'>{0}</span>", contact.Countryname);
            html.Append("</div>");
            }
            if (!string.IsNullOrEmpty(contact.Phone))
                html.AppendFormat("<div class='tel'>{0}</div>", contact.Phone);
            if (!string.IsNullOrEmpty(contact.Url))
                html.AppendFormat("<div><a href='{0}' class='url'>{0}</a></div>", contact.Url);
            html.Append("</div>");
            
            return html.ToString();

        }
        private Contact GetContent(IWin32Window dialogOwner)
        {
            using (contactForm form = new contactForm())
            {
                Contact c = null;
                DialogResult result = form.ShowDialog(dialogOwner);

                if ((result == DialogResult.OK) && (form.c !=null))
                {
                    c = form.c;
                }
                return c;
            }
        }
    }
    public class Contact : INotifyPropertyChanged

    {
        public Contact()
        {

        }
        private string givenname;

        public string Givenname
        {
            get { return givenname; }
            set
            {
                givenname = value; NotifyPropertyChanged("Givenname");
            }
        }
        private string middlename;

        public string Middlename
        {
            get { return middlename; }
            set { middlename = value; NotifyPropertyChanged("Middlename"); }
        }
        private string familyname;

        public string Familyname
        {
            get { return familyname; }
            set { familyname = value; NotifyPropertyChanged("Familyname"); }
        }
        private string organization;

        public string Organization
        {
            get { return organization; }
            set { organization = value; NotifyPropertyChanged("Organization"); }
        }
        private string street;

        public string Street
        {
            get { return street; }
            set { street = value; NotifyPropertyChanged("Street"); }
        }
        private string city;

        public string City
        {
            get { return city; }
            set { city = value; NotifyPropertyChanged("City"); }
        }
        private string state;

        public string State
        {
            get { return state; }
            set { state = value; NotifyPropertyChanged("State"); }
        }
        private string postalcode;

        public string Postalcode
        {
            get { return postalcode; }
            set { postalcode = value; NotifyPropertyChanged("Postalcode"); }
        }
        private string countryname;

        public string Countryname
        {
            get { return countryname; }
            set { countryname = value; NotifyPropertyChanged("Countryname"); }
        }
        private string phone;

        public string Phone
        {
            get { return phone; }
            set { phone = value; NotifyPropertyChanged("Phone"); }
        }
        private string email;

        public string Email
        {
            get { return email; }
            set { email = value; NotifyPropertyChanged("Email"); }
        }
        private string url;

        public string Url
        {
            get { return url; }
            set { url = value; NotifyPropertyChanged("Url"); }
        }
        private string photourl;

        public string Photourl
        {
            get { return photourl; }
            set { photourl = value; NotifyPropertyChanged("Photourl"); }
        }



        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }


    }
}